/*
 * @Author: szx
 * @Date: 2022-04-16 16:44:10
 * @LastEditTime: 2022-04-16 16:55:49
 * @Description:
 * @FilePath: \leetcode\剑指offerII\067\067.js
 */
/**
 * @param {number[]} nums
 * @return {number}
 */
/**
 * @param {number[]} nums
 * @return {number}
 */
var findMaximumXOR = function (nums) {
    const trie = {};
    // 建立前缀树
    for (let i of nums) {
        let node = trie;
        for (let j = 30; j >= 0; j--) {
            let attr = (i >> j) & 1;
            if (!node[attr]) {
                node[attr] = {};
            }
            node = node[attr];
        }
    }
    let res = 0;
    for (let i of nums) {
        let node = trie;
        let result = 0;
        for (let j = 30; j >= 0; j--) {
            let bit = (i >> j) & 1;
            let index = bit ^ 1;
            if (node[index]) {
                result |= 1 << j;
                node = node[index];
            } else {
                node = node[bit];
            }
        }
        res = Math.max(res, result);
    }
    return res;
};
